﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Partial Class staff
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            If txtSearch.Text = "" Then
                LoadStaff()
            Else
                LoadStaff(txtSearch.Text)
            End If
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            If txtSearch.Text = "" Then
                LoadStaff()
            Else
                LoadStaff(txtSearch.Text)
            End If
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        If txtSearch.Text = "" Then
            LoadStaff()
        Else
            LoadStaff(txtSearch.Text)
        End If
    End Sub

#End Region

    Private Sub LoadStaff(Optional ByVal strName As String = "")

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        If strName = "" Then
            objDB.dbVariable.SQLString = "SELECT StaffId,Name, CASE WHEN StaffType=1 THEN 'BOSS' WHEN StaffType=2 THEN 'REN' WHEN StaffType=4 THEN 'PROBATION' WHEN StaffType=5 THEN 'RECEPTIONIST' WHEN StaffType=6 THEN 'ACCOUNT' WHEN StaffType=7 THEN 'PART-TIME' ELSE 'ADMIN' END AS StaffType, Phone, Mobile, Email, Ren FROM AW_Staff WHERE StatusDelete=0" & lblHidSortType.Text
            objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)
        Else
            objDB.dbVariable.SQLString = "SELECT StaffId,Name, CASE WHEN StaffType=1 THEN 'BOSS' WHEN StaffType=2 THEN 'REN' WHEN StaffType=4 THEN 'PROBATION' WHEN StaffType=5 THEN 'RECEPTIONIST' WHEN StaffType=6 THEN 'ACCOUNT' WHEN StaffType=7 THEN 'PART-TIME' ELSE 'ADMIN' END AS StaffType, Phone, Mobile, Email, Ren FROM AW_Staff WHERE StatusDelete=0 AND Name LIKE @Name" & lblHidSortType.Text
            objDB.AddParameter("Name", Data.SqlDbType.NVarChar, "%" & strName & "%")
            objDB.FillData(TempDT, "AW_Country", Database.SQLCommandType.NormalString)
        End If
        objDB.CloseConnection()

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repStaff.DataSource = objPaged
        repStaff.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub imgNewStaff_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewStaff.Click
        Response.Redirect("staffentry.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "1" Then 'boss
            lblStaffUID.Text = Session("StaffId").ToString
        Else
            Response.Redirect("Dashboard.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '0-Back Page
            '1-Search
            '3-Page Size
            '4-Current Page
            If arr(0).ToString.Contains("staff.aspx") Then
                txtSearch.Text = arr(1).ToString
                PageSize = CInt(arr(2).ToString)
                CurrentPage = CInt(arr(3).ToString)
                Session.Remove("Back")
                LoadStaff()
            Else
                Session.Remove("Back")
            End If
        End If

        If Not Page.IsPostBack Then
            LoadStaff()
            If Not Session.Item("entry") Is Nothing Then
                lblMsgSuccessful.Text = "<strong>Information:</strong>Staff <strong>" & Session.Item("entry").ToString & "</strong> was created"
                divSuccessful.Visible = True
            End If

            If Not Session.Item("entryU") Is Nothing Then
                lblMsgSuccessful.Text = "<strong>Information:</strong>Staff <strong>" & Session.Item("entryU").ToString & "</strong> was updated"
                divSuccessful.Visible = True
            End If
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub repStaff_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repStaff.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            'Back Page
            'Search           
            Dim strBack As String
            strBack = "staff.aspx" & "ڡ" & txtSearch.Text.Trim & "ڡ" & PageSize & "ڡ" & CurrentPage
            Session.Add("Back", strBack)
            Dim staffUID As String = CType(repStaff.Items(e.CommandArgument).FindControl("lblRepStaffUID"), Label).Text
            Response.Redirect("staffentry.aspx?id=" & SymmetricEncryptToHex(staffUID, "augment"))
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "UPDATE AW_Staff SET StatusDelete=1 WHERE StaffId=@StaffId"
            objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repStaff.Items(e.CommandArgument).FindControl("lblRepStaffUID"), Label).Text))
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            lblMsgSuccessful.Text = "Staff Deleted"
            lblMsgSuccessful.Visible = True
            If txtSearch.Text <> "" Then
                LoadStaff(txtSearch.Text)
            Else
                LoadStaff()
            End If
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkName.Click
        If lnkName.Text = "Staff Name▼" Then
            lnkName.Text = "Staff Name▲"
        Else
            lnkName.Text = "Staff Name▼"
        End If

        lblHidSortType.Text = " ORDER BY Name " & SortType(lnkName.Text) & ", StaffType " & SortType(lnkStaffType.Text) & _
                            ", Phone " & SortType(lnkPhone.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub lnkStaffType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkStaffType.Click
        If lnkStaffType.Text = "Staff Type▼" Then
            lnkStaffType.Text = "Staff Type▲"
        Else
            lnkStaffType.Text = "Staff Type▼"
        End If

        lblHidSortType.Text = " ORDER BY StaffType " & SortType(lnkStaffType.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", Phone " & SortType(lnkPhone.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub lnkPhone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPhone.Click
        If lnkPhone.Text = "Phone▼" Then
            lnkPhone.Text = "Phone▲"
        Else
            lnkPhone.Text = "Phone▼"
        End If

        lblHidSortType.Text = " ORDER BY Phone " & SortType(lnkPhone.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", StaffType " & SortType(lnkStaffType.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub lnkMobile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMobile.Click
        If lnkMobile.Text = "Mobile▼" Then
            lnkMobile.Text = "Mobile▲"
        Else
            lnkMobile.Text = "Mobile▼"
        End If

        lblHidSortType.Text = " ORDER BY Mobile " & SortType(lnkMobile.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", StaffType " & SortType(lnkStaffType.Text) & ", Phone " & SortType(lnkPhone.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub lnkEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkEmail.Click
        If lnkEmail.Text = "Email▼" Then
            lnkEmail.Text = "Email▲"
        Else
            lnkEmail.Text = "Email▼"
        End If

        lblHidSortType.Text = " ORDER BY Email " & SortType(lnkEmail.Text) & ", Name " & SortType(lnkName.Text) &
                            ", StaffType " & SortType(lnkStaffType.Text) & ", Phone " & SortType(lnkPhone.Text) &
                            ", Mobile " & SortType(lnkMobile.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub lnkRen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRen.Click
        If lnkRen.Text = "Ren▼" Then
            lnkRen.Text = "Ren▲"
        Else
            lnkRen.Text = "Ren▼"
        End If

        lblHidSortType.Text = " ORDER BY Ren " & SortType(lnkEmail.Text) & ", Name " & SortType(lnkName.Text) &
                            ", StaffType " & SortType(lnkStaffType.Text) & ", Phone " & SortType(lnkPhone.Text) &
                            ", Mobile " & SortType(lnkMobile.Text)
        LoadStaff(txtSearch.Text)
    End Sub

    Protected Sub repStaff_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repStaff.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            CType(e.Item.FindControl("cbeDelete"), Object).confirmText = "Are you sure you want to delete " & CType(e.Item.FindControl("lblRepStaffName"), Label).Text
        End If
    End Sub
End Class
